import axios from 'axios'
import Vue from 'vue'
import router from '../router'

const http = axios.create({
    baseURL: process.env.VUE_APP_API_URL || '/admin/api'
    // baseURL: "http://localhost:3000/admin/api"
});

http.interceptors.request.use(config => {
    if (localStorage.getItem('token')) {
        config.headers.Authorization = 'Bearer ' + localStorage.getItem('token');
    }
    return config;
})

http.interceptors.response.use(config => {
    return config;
}, error => {
    if (error.response.status === 401) {
        Vue.prototype.$message.error(error.response.data.msg);
        router.push('/login');
    }
})

export default http
